package com.llb666.shop.items.infrastructure.persistence;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.llb666.common.core.utils.page.PagedGridResult;
import com.llb666.shop.items.api.vo.SearchItemsVo;
import com.llb666.shop.items.domain.entity.Items;
import com.llb666.shop.items.domain.entity.bo.ItemsCommentBo;
import com.llb666.shop.items.domain.repository.ItemsRepository;
import com.llb666.shop.items.infrastructure.conver.ItemsCvt;
import com.llb666.shop.items.infrastructure.mapper.ItemsCommentsMapper;
import com.llb666.shop.items.infrastructure.mapper.ItemsMapper;
import com.llb666.shop.items.infrastructure.po.ItemsCommentsPo;
import com.llb666.shop.items.infrastructure.po.ItemsPo;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

/**
 * 商品仓储层实现类.
 *
 * @author llb666
 */
@Repository
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class ItemsRepositoryImpl implements ItemsRepository {

    private final ItemsMapper itemsMapper;
    private final ItemsCommentsMapper itemsCommentsMapper;

    /**
     * 根据一级商品分类查询商品信息.取前6条，按照创建时间倒序
     *
     * @param rootCatIdSet 一级商品分类列表
     * @return 商品信息列表
     */
    @Override
    public List<Items> getItemsByRootCatIdSet(Set<Integer> rootCatIdSet) {
        Example example = new Example(ItemsPo.class);
        example.orderBy("createdTime").desc();

        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("rootCatId", rootCatIdSet);

        RowBounds rowBounds = new RowBounds(0, 6);
        return ItemsCvt.INSTANCE.po2doList(itemsMapper.selectByExampleAndRowBounds(example, rowBounds));
    }

    /**
     * 根据商品id和评论等级查询商品评论数.
     *
     * @param id    商品id
     * @param level 评论等级
     * @return 商品评论
     */
    @Override
    public Integer getCommentCount(String id, Integer level) {
        ItemsCommentsPo condition = new ItemsCommentsPo();
        condition.setItemId(id);
        if (level != null) {
            condition.setCommentLevel(level);
        }
        return itemsCommentsMapper.selectCount(condition);
    }

    /**
     * 查询商品详情.
     *
     * @param itemsId 商品id
     * @return 商品基础信息
     */
    @Override
    public Items queryById(String itemsId) {
        return ItemsCvt.INSTANCE.po2do(itemsMapper.selectByPrimaryKey(itemsId));
    }


    /**
     * 商品评论查询.
     *
     * @param paramsMap 商品id, 评论等级
     * @param page      -
     * @param size      -
     * @return 商品评论
     */
    @Override
    public PagedGridResult<ItemsCommentBo> queryItemComments(Map<String, Object> paramsMap, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<ItemsCommentBo> itemCommentBos = itemsCommentsMapper.queryItemComments(paramsMap);
        PageInfo<ItemsCommentBo> itemCommentBoPageInfo = new PageInfo<>(itemCommentBos);
        return PagedGridResult.setterPagedGrid(itemCommentBoPageInfo);
    }

    /**
     * 根据商品id列表查询商品信息
     *
     * @param itemIdSet 商品id列表
     * @return 商品列表信息
     */
    @Override
    public List<Items> getItemsListByIds(Set<String> itemIdSet) {
        Example example = new Example(ItemsPo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", itemIdSet);
        return ItemsCvt.INSTANCE.po2doList(itemsMapper.selectByExample(example));
    }

    /**
     * 根据商品id列表查询商品信息.
     *
     * @param itemIds 商品id
     * @return 商品列表
     */
    @Override
    @Async("itemsServiceExecutor")
    public CompletableFuture<List<Items>> getItemsFutureByIds(List<String> itemIds) {
        Example example = new Example(ItemsPo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", itemIds);
        List<Items> items = ItemsCvt.INSTANCE.po2doList(itemsMapper.selectByExample(example));
        return CompletableFuture.completedFuture(items);
    }

    /**
     * 通过分类id搜索商品列表
     *
     * @param catId 类目id
     * @param sort  排序
     * @param page  -
     * @param size  -
     * @return 商品列表
     */
    @Override
    public PagedGridResult<SearchItemsVo> searchItemsByThirdCat(Integer catId, String sort, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<SearchItemsVo> searchItemsVos = itemsMapper.searchItemsByThirdCat(catId, sort);
        PageInfo<SearchItemsVo> searchItemsVoPageInfo = new PageInfo<>(searchItemsVos);
        return PagedGridResult.setterPagedGrid(searchItemsVoPageInfo);
    }
}
